﻿using BaseFrame.Tool.CefSharp.FileResource;
using CefSharp;
using CefSharp.Handler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseFrame.Tool.CefSharp
{
    /// <summary>
    /// 服务端返回资源自定义处理
    /// </summary>
    public class CustomResourceResourceRequestHandler : ResourceRequestHandler
    {
        protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
        {
            var filter = FilterManager.CreateFilter(request.Identifier.ToString());//将请求的资源获取到
            return filter;
        }
        protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
        {
            if (request.Url.Contains("请求资源的url"))
            {
                TestJsonFilter filter = FilterManager.GetFileter(request.Identifier.ToString()) as TestJsonFilter;//将资源转换得到
                if (filter != null)//自定义处理
                {
                    //var arryImg = filter.DataAll.ToArray();//转为byte[]
                    //string strResult = System.Text.Encoding.UTF8.GetString(arryImg);
                    //if (!string.IsNullOrEmpty(strResult)) {
                    //    JObject jo = (JObject)JsonConvert.DeserializeObject(strResult);
                    //    if (jo == null || jo["data"] == null)
                    //        return;
                    //    ObjectEntity viewEntity =
                    //        JsonConvert.DeserializeObject<ObjectEntity>(jo["data"].ToString());
                    //}
                }
            }
        }
    }
}
